/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.sys.web;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.thinkgem.jeesite.common.persistence.BasePage;
import com.thinkgem.jeesite.common.utils.DateUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.sys.entity.SysLog;
import com.thinkgem.jeesite.modules.sys.service.SysLogService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 日志Controller
 *
 * @author kongxiangsheng
 * @version 2013-6-2
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/sysLog")
public class SysLogController extends BaseController {

    @Autowired
    private SysLogService sysLogService;

    @RequiresPermissions("sys:sysLog:view")
    @RequestMapping(value = {"list", ""})
    public String list(SysLog sysLog, HttpServletRequest request, HttpServletResponse response, Model model) {
        BasePage<SysLog> page = new BasePage<SysLog>(request, response);

        if (StringUtils.isBlank(sysLog.getCreateBy())) {
            sysLog.setCreateBy(null);
        }
        if (StringUtils.isBlank(sysLog.getTitle())) {
            sysLog.setTitle(null);
        }
        if (StringUtils.isBlank(sysLog.getRequestUri())) {
            sysLog.setRequestUri(null);
        }

        QueryWrapper queryWrapper = new QueryWrapper<SysLog>(sysLog);
        // 设置默认时间范围，默认当前月
        if (sysLog.getBeginDate() == null) {
            sysLog.setBeginDate(DateUtils.setDays(DateUtils.parseDate(DateUtils.getDate()), 1));
        }
        if (sysLog.getEndDate() == null) {
            sysLog.setEndDate(DateUtils.addMonths(sysLog.getBeginDate(), 1));
        }
        queryWrapper.between(SysLog.COL_CREATE_DATE, sysLog.getBeginDate(), sysLog.getEndDate());
        sysLogService.page(page, queryWrapper);

        model.addAttribute("page", page);
        return "modules/sys/SysLog/list";
    }

}
